﻿using System;
using System.Runtime.Remoting.Messaging;

namespace Sofire.AOP
{
    /// <summary>
    /// 在“实例化对象”、“设置或获取字段”、“设置或获取属性”、“注册或注销事件”或“调用方法”后发生的消息。
    /// </summary>
    public class LeaveInvokeMessage : EnterInvokeMessage
    {
        private IMethodReturnMessage _Return;
        /// <summary>
        /// 获取一个值，表示返回的消息。
        /// </summary>
        public IMethodReturnMessage Return
        {
            get
            {
                return this._Return;
            }
        }

        private object _Value;
        /// <summary>
        /// 获取一个值，表示返回的值。
        /// </summary>
        public object Value
        {
            get
            {
                return this._Value;
            }
        }

        //private NewReturnMessage _NewReturn;
        //public NewReturnMessage NewReturn
        //{
        //    get
        //    {
        //        return this._NewReturn;
        //    }
        //    set
        //    {
        //        this._NewReturn = value;
        //    }
        //}

        internal LeaveInvokeMessage(object instance, CallAction action, IMethodCallMessage call, IMethodReturnMessage @return)
            : base(instance, action, call)
        {
            this._Return = @return;

            switch(action)
            {
                case CallAction.FieldSetter:
                case CallAction.FieldGetter:

                    this._Value = this._Return.GetArg(2);
                    break;

                case CallAction.PropertySetter:
                case CallAction.EventAdder:
                case CallAction.EventRemover:
                    this._Value = this._Return.GetArg(0);
                    break;

                case CallAction.Constructor:
                case CallAction.PropertyGetter:
                    this._Value = this._Return.ReturnValue;
                    break;

                case CallAction.InvokeMethod:
                default:
                    this._Value = this._Return.ReturnValue;

                    var methodInfo = this._Return.MethodBase as System.Reflection.MethodInfo;
                    if(methodInfo != null && methodInfo.ReturnType == Types.Void) this._Value = null;

                    break;
            }
        }

        //internal IMethodReturnMessage GetReturnMessage()
        //{
        //   // if(this._NewReturn == null) 
        //        return this._Return;
            
           
        //}
    }

    //public class NewReturnMessage
    //{
    //    public NewReturnMessage(object value,object[] outArgs)
    //    {
    //        this._Value = value;
    //        this.OutArgs = outArgs;
    //    }

    //    private object _Value;
    //    public object Value
    //    {
    //        get
    //        {
    //            return this._Value;
    //        }
    //        set
    //        {
    //            this._Value = value;
    //        }
    //    }

    //    private object[] _OutArgs;
    //    public object[] OutArgs
    //    {
    //        get
    //        {
    //            return this._OutArgs;
    //        }
    //        set
    //        {
    //            if(value == null) value = new object[0];
    //            this.OutArgs = value;
    //        }
    //    }
    //}
}
